﻿using AutoMapper;
using MediatR;
using MES.Bom.API.Application.Command.物料命令;
using MES.Bom.Domain;
using MES.Bom.ErrorCode;
using MES.Bom.Infrastructure;

namespace MES.Bom.API.Application.CommandHandler.物料命令处理
{
    /// <summary>
    /// 物料分配工序命令处理器
    /// </summary>
    public class MaterailProcessAddCommandHandler : IRequestHandler<MaterailProcessAddCommand, APIResult<int>>
    {
        private readonly IBaseRepository<MaterialProcess> mpRepository;
        private readonly ILogger<MaterailProcessAddCommandHandler> logger;
        private readonly IMapper mapper;
        public MaterailProcessAddCommandHandler(IBaseRepository<MaterialProcess> mpRepository, ILogger<MaterailProcessAddCommandHandler> logger = null, IMapper mapper = null)
        {
            this.mpRepository = mpRepository;
            this.logger = logger;
            this.mapper = mapper;
        }

        public async Task<APIResult<int>> Handle(MaterailProcessAddCommand request, CancellationToken cancellationToken)
        {
            try
            {
                APIResult<int> result = new APIResult<int>();
                result.Code = APIEnums.Success;
                result.Message = "添加成功";
                var model=mapper.Map<MaterialProcess>(request);
                result.Data=await mpRepository.AddAsync(model);
                return Task.FromResult(result).Result;
            }
            catch (Exception ex)
            {

                throw;
            }
        }
    }
}
